package com.kukajenny.bobozan;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by zjp on 17-9-21.
 */

public class GameActivity extends Activity{

    int left_id [] = {R.drawable.left_attack,R.drawable.left_protect,R.drawable.left_refuel};
    int right_id [] = {R.drawable.right_attack,R.drawable.right_protect,R.drawable.right_refuel};

    int left_action [] = {2,2,0,0};
    int right_action [] = {2,1,1,2};

    int left_round = 0;
    int right_round = 0;

    ImageView left_iv,right_iv;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //left_iv.setImageDrawable(getResources().getDrawable(left_id[left_action[round]]));
                   // right_iv.setImageResource(right_id[right_action[round]]);
                    //round++;
                    //Toast.makeText(GameActivity.this,round + " "+ left_action.length+" "+right_action.length,Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        left_iv = (ImageView) findViewById(R.id.left_iv);
        right_iv = (ImageView) findViewById(R.id.right_iv);

        AnimationDrawable animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(left_id[0]), 100);
        animation.addFrame(getResources().getDrawable(left_id[1]), 500);
        animation.addFrame(getResources().getDrawable(left_id[2]), 300);
        animation.setOneShot(false);


        left_iv.setBackgroundDrawable(animation);

        // start the animation!
        animation.start();


        /*new CountDownTimer(1000,2000) {

            @Override
            public void onTick(long millisUntilFinished) {}

            @Override
            public void onFinish() {
                if(left_round<4) {
                    left_iv.setImageResource(left_id[left_action[left_round]]);
                    //right_iv.setImageResource(right_id[right_action[round]]);
                }
                left_round++;

                start();
            }
        }.start();

        new CountDownTimer(1000,2000) {

            @Override
            public void onTick(long millisUntilFinished) {}

            @Override
            public void onFinish() {
                if(right_round<4) {
                    //left_iv.setImageResource(left_id[left_action[round]]);
                    right_iv.setImageResource(right_id[right_action[right_round]]);
                }
                right_round++;

                start();
            }
        }.start();*/

        /*Runnable runnable = new Runnable(){

            @Override
            public void run() {
// TODO Auto-generated method stub
                //Toast.makeText(GameActivity.this,round+"",Toast.LENGTH_SHORT).show();
                left_iv.setImageResource(left_id[left_action[round]]);
                right_iv.setImageResource(right_id[right_action[round]]);
                handler.postDelayed(this, 1000);
                *//*left_iv.setImageResource(R.drawable.logo);
                right_iv.setImageResource(R.drawable.logo);
                handler.postDelayed(this, 50);*//*
                round++;
                if(round >= left_action.length || round >= right_action.length){
                    Toast.makeText(GameActivity.this,"fds",Toast.LENGTH_SHORT).show();
                    handler.removeCallbacks(this);
                }

            }

        };

        handler.postDelayed(runnable, 2000);// 打开定时器，执行操作*/

        /*while (round < left_action.length && round < right_action.length){
            *//*if(round == 0){
                left_iv.setImageDrawable(getResources().getDrawable(left_id[left_action[round]]));
                right_iv.setImageResource(right_id[right_action[round]]);
            }*//*

            TimerTask task = new TimerTask(){
                public void run() {
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                }
            };
            Timer timer = new Timer(true);
            timer.schedule(task, 0, 3000);//延时0s执行,每100ms执行一次

            Log.d("mylog",round+"");

        }*/
    }


}
